package EGS.impl;
import java.util.concurrent.ExecutorService;

import javax.swing.JTable;

import org.dom4j.Document;
import org.dom4j.Element;

import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.stationClient;

public class EGS_execute_service10013Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 10013 获取产品BIN描述信息
	 */
	public EGS_execute_service10013Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String transactionID = Mroot.element("header").attribute("transactionID").getValue();
			String iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if (msgClass.equals("10013")) {// 获取产品BIN描述信息
				String partNumber, version, bin, orderNumber;
				try {
					partNumber = Mroot.element("body").element("product").attribute("number").getValue();// 产品编号
				} catch (Exception e) {
					partNumber = "";
				}
				try {
					version = Mroot.element("body").element("product").attribute("version").getValue();// 产品BIN
				} catch (Exception e) {
					version = "";
				}
				try {
					bin = Mroot.element("body").element("product").attribute("bin").getValue();// 产品BIN
				} catch (Exception e) {
					bin = "";
				}
				try {
					orderNumber = Mroot.element("body").element("order").attribute("number").getValue();// 工单号
				} catch (Exception e) {
					orderNumber = "";
				}

				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				Element eleP = eleB.addElement("product");

				String sqlstr = "";

				try {
					if ((partNumber == null || partNumber.length() < 1) || (version == null || version.length() < 1)
							|| (bin == null || bin.length() < 1)) {
						if (orderNumber == null || orderNumber.length() < 1) {
							throw new Exception("查询条件不全，必须要有工单编号或者完整的产品信息");
						} else {
							sqlstr = "select masterOrderNumber,partNumber,version,BSindex from vOrderList_new where orderNumber='"
									+ orderNumber + "'";
							JTable t = sql.select(sqlstr);
							if (t != null && t.getRowCount() > 0) {
								String iOrder = (String) t.getValueAt(0, 0);
								if (!iOrder.equals(orderNumber)) {
									orderNumber = iOrder;
									sqlstr = "select masterOrderNumber,partNumber,version,BSindex from vOrderList_new where orderNumber='"
											+ orderNumber + "'";
									t = sql.select(sqlstr);
								}
								partNumber = (String) t.getValueAt(0, 1);
								version = (String) t.getValueAt(0, 2);
								bin = (String) t.getValueAt(0, 3);
							} else {
								sqlstr = "SELECT olp.panelOrderNumber,bvs.panelNumber,bvs.panelVersion,olp.panelBIN \r\n" + 
										"FROM [dbo].[order_list_panel] olp \r\n" + 
										"LEFT JOIN dbo.order_list ol ON ol.orderIndex=olp.orderIndex \r\n" + 
										"LEFT JOIN BOM.vStruct bvs ON bvs.panelNumber=olp.panelNumber and bvs.partIndex=ol.masterIndex and bvs.partVersion=ol.version\r\n" + 
										"where olp.panelOrderNumber='"+orderNumber+"' and ol.orderNumber!=ol.masterOrderNumber ";
								t = sql.select(sqlstr);
								if (t != null && t.getRowCount() > 0) {
									partNumber = (String) t.getValueAt(0, 1);
									version = (String) t.getValueAt(0, 2);
									bin = (String) t.getValueAt(0, 3);
								}else {
									throw new Exception("工单号不存在");
								}
							}
						}
					}
					if ((partNumber == null || partNumber.length() < 1) || (version == null || version.length() < 1)
							|| (bin == null || bin.length() < 1)) {
						throw new Exception("产品BIN号描述基础信息不存在");
					} else {
						int binIndex = Integer.parseInt(bin);
						sqlstr = "select desc1,desc2,desc3,desc4,isRelease "
								+ "from dbo.product_BINpartViewer where masterNumber='" + partNumber
								+ "' and version='" + version + "' and BSIndex=" + binIndex;
						JTable t = sql.select(sqlstr);
						String bin_laser = "", bin_label = "", bin_package = "" ,bin_FCT="";// 多种LED的情况
						int irow = t.getRowCount();
						for (int i = 0; i < irow; i++) {
							if (i > 0) {
								bin_laser = bin_laser + ",";
								bin_label = bin_label + ",";
								bin_package = bin_package + ",";
								bin_FCT = bin_FCT + ",";
							}
							bin_laser = bin_laser + (String) t.getValueAt(i, 0);
							bin_label = bin_label + (String) t.getValueAt(i, 1);
							bin_package = bin_package + (String) t.getValueAt(i, 2);
							bin_FCT = bin_FCT + (String) t.getValueAt(i, 3);
							String isRelease=""+t.getValueAt(i, 4);
							if(isRelease!=null && isRelease.equals("0")) {
								throw new Exception("BIN描述未审核");
							}
						}

						eleP.addAttribute("number", partNumber);
						eleP.addAttribute("version", version);
						eleP.addAttribute("bin", bin);
						eleP.addAttribute("bin_laser", "" + bin_laser);
						eleP.addAttribute("bin_label", "" + bin_label);
						eleP.addAttribute("bin_package", "" + bin_package);
						eleP.addAttribute("bin_FCT", "" + bin_FCT);
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						eleR.addAttribute("actionCode", "0");
						AKSLogs(100131, "" + partNumber + "_" + version + "_" + bin + "_" + iStation);
					}

				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(100130, "" + partNumber + "_" + version + "_" + bin + "_" + iStation + "_" + str);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
